package com.mvc.controller;
/*	@Autowired
	private DictService dictService;

	List fllist=dictService.getAllByKeyname("文章分类");		
	modelMap.put("fllist",fllist); 	
		
		
	#foreach($lm in $fllist)
	   <option value="$!{lm.getOptvalue()}">$!{lm.getOptname()}</option>
	#end 
	*/
import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.mvc.entity.Dict;
import com.mvc.entity.Meminfo;
import com.mvc.service.DictService;
import com.pub.Page;

@Controller
@RequestMapping("Dict")
public class DictController {
	protected final transient Log logger = LogFactory
			.getLog(DictController.class);

	@Autowired
	private DictService dictService;

	public DictController() {
	}

	/* 角色管理 */
	@RequestMapping("Index.do")
	public String Index(HttpSession session,ModelMap modelMap, String pageNo, String Ajax) {
		Meminfo smeminfo=(Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo",smeminfo);
		Page page = new Page();
		page.setPageNox(pageNo);
		page.setPageSize(20);

		String hql = "select a from Dict a where a.Optname='请选择' order by a.ID desc";
		List DictList = dictService.getDictList(hql, page);
		String counthql = "select count(*) from  Dict a where a.Optname='请选择'";
		page.setPageCount(dictService.getCount(counthql, page));
		modelMap.put("page", page);
		modelMap.put("DictList", DictList);
		if (Ajax != null && Ajax.equals("Ajax")) {
			return "/Dict/List";
		} else {
			return "/Dict/Index";
		}

	}

	@RequestMapping("Add.do")
	public String Add(HttpSession session,ModelMap modelMap) {
		Meminfo smeminfo=(Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo",smeminfo);
		return "/Dict/Add";
	}

	/* 保存 */
	@RequestMapping("Save.do")
	public void Save(HttpSession session,HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap) {
		Meminfo smeminfo=(Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo",smeminfo);
		String str = "{\"statusCode\":200,\"message\":\""
				+ request.getParameter("Keyname")
				+ "新增成功\",\"callbackType\":\"forward\",\"forwardUrl\":\"/Dict/Index.do\"}";
		try {
			if (dictService.IsExistDict(request.getParameter("Keyname"))) {
				Dict dict = new Dict();
				dict.setKeyname(request.getParameter("Keyname").trim());
				dict.setKeycontext(request.getParameter("Keycontext").trim());
				dict.setOptname("请选择");
				dict.setOptvalue("请选择");
				dict.setMyorder(0);
				dictService.Save(dict);
			} else {
				str = "{\"statusCode\":300,\"message\":\""
						+ request.getParameter("Keyname")
						+ "新增失败\",\"callbackType\":\"forward\",\"forwardUrl\":\"\"}";
			}
			response.getWriter().print(str);
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}
	}

	/* 保存 */
	@RequestMapping("EditSave.do")
	public void EditSave(HttpSession session,HttpServletResponse response,
			HttpServletRequest request, ModelMap modelMap) {
		Meminfo smeminfo=(Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo",smeminfo);
		String str = "{\"statusCode\":200,\"message\":\""
				+ request.getParameter("Keyname")
				+ "修改成功\",\"callbackType\":\"forward\",\"forwardUrl\":\"/Dict/Index.do\"}";
		try {
			String oldKeyname = request.getParameter("Oldkeyname");
			String Keyname = request.getParameter("Keyname");
			String Keycontext = request.getParameter("Keycontext");
			String hql = "update Dict a set a.Keyname='" + Keyname
					+ "',a.Keycontext='" + Keycontext + "' where a.Keyname='"
					+ oldKeyname + "'";
			dictService.EditBySql(hql);

			response.getWriter().print(str);
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}
	}

	/* 删除 */
	@RequestMapping("Del.do")
	public void Del(HttpSession session,HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap) {
		Meminfo smeminfo=(Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo",smeminfo);
		String keyname = request.getParameter("Keyname");
		String str = "{\"statusCode\":200,\"message\":\""
				+ keyname
				+ "删除成功\",\"callbackType\":\"forward\",\"forwardUrl\":\"/Dict/Index.do\"}";

		dictService.DelDict(keyname);
		try {
			response.getWriter().print(str);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@RequestMapping("Edit.do")
	public String Edit(HttpSession session,HttpServletRequest request, ModelMap modelMap) {
		Meminfo smeminfo=(Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo",smeminfo);
		String ID = request.getParameter("ID");
		Dict dict = dictService.getDict(ID);
		modelMap.put("dict", dict);
		return "/Dict/Edit";
	}

	@RequestMapping("Options.do")
	public String Options(HttpSession session,HttpServletRequest request, ModelMap modelMap) {
		Meminfo smeminfo=(Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo",smeminfo);
		String ID = request.getParameter("ID");
		Dict dict = dictService.getDict(ID);
		List list = dictService.getAllByKeyname(dict.getKeyname());
		modelMap.put("allList", list);
		modelMap.put("Keyname", dict.getKeyname());
		modelMap.put("ID", ID);
		return "/Dict/Options";
	}

	@RequestMapping("AddOptions.do")
	public String AddOptions(HttpSession session,HttpServletRequest request, ModelMap modelMap) {
		Meminfo smeminfo=(Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo",smeminfo);
		modelMap.put("ID", request.getParameter("ID"));
		modelMap.put("Keyname", request.getParameter("Keyname"));
		return "/Dict/AddOptions";
	}

	@RequestMapping("DelOptionsForm.do")
	public String DelOptionsForm(HttpSession session,HttpServletRequest request, ModelMap modelMap)

	{	
		Meminfo smeminfo=(Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo",smeminfo);
		modelMap.put("ID", request.getParameter("ID"));
		modelMap.put("DID", request.getParameter("DID"));
		modelMap.put("Keyname", request.getParameter("Keyname"));
		Dict dict = dictService.getDict(request.getParameter("DID"));
		modelMap.put("Dict", dict);
		return "/Dict/DelOptionsForm";
	}

	@RequestMapping("EditOptionsForm.do")
	public String EditOptionsForm(HttpSession session,HttpServletRequest request, ModelMap modelMap)

	{	
		Meminfo smeminfo=(Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo",smeminfo);
		modelMap.put("ID", request.getParameter("ID"));
		modelMap.put("DID", request.getParameter("DID"));
		modelMap.put("Keyname", request.getParameter("Keyname"));
		Dict dict = dictService.getDict(request.getParameter("DID"));
		modelMap.put("Dict", dict);
		return "/Dict/EditOptionsForm";
	}

	/* 保存 */
	@RequestMapping("OptionSave.do")
	public void OptionSave(HttpSession session,HttpServletResponse response,
			HttpServletRequest request, ModelMap modelMap) {
		Meminfo smeminfo=(Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo",smeminfo);
		String str = "{\"statusCode\":200,\"message\":\""
				+ request.getParameter("Keyname")
				+ "-"
				+ request.getParameter("Optname")
				+ "新增成功\",\"callbackType\":\"forward\",\"forwardUrl\":\"/Dict/Options.do?ID="
				+ request.getParameter("ID") + "\"}";
		try {

			Dict dict = new Dict();
			dict.setKeyname(request.getParameter("Keyname").trim());
			dict.setKeycontext("无描述");
			dict.setOptname(request.getParameter("Optname").trim());
			dict.setOptvalue(request.getParameter("Optvalue").trim());
			dict.setMyorder(0);
			dictService.Save(dict);

			response.getWriter().print(str);
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}
	}

	/* 保存 */
	@RequestMapping("EditOptionSave.do")
	public void EditOptionSave(HttpSession session,HttpServletResponse response,
			HttpServletRequest request, ModelMap modelMap) {
		Meminfo smeminfo=(Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo",smeminfo);
		String str = "{\"statusCode\":200,\"message\":\""
				+ request.getParameter("Keyname")
				+ "-"
				+ request.getParameter("Optname")
				+ "修改成功\",\"callbackType\":\"forward\",\"forwardUrl\":\"/Dict/Options.do?ID="
				+ request.getParameter("ID") + "\"}";
		try {

			Dict dict = dictService.getDict(request.getParameter("DID"));
			dict.setKeyname(request.getParameter("Keyname").trim());
			// dict.setKeycontext("无描述");
			dict.setOptname(request.getParameter("Optname").trim());
			dict.setOptvalue(request.getParameter("Optvalue").trim());
			// dict.setMyorder(0);
			dictService.EditSave(dict);

			response.getWriter().print(str);
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}
	}

	/* 删除 */
	@RequestMapping("DelOptions.do")
	public void DelOptions(HttpSession session,HttpServletResponse response,
			HttpServletRequest request, ModelMap modelMap) {
		Meminfo smeminfo=(Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo",smeminfo);
		String DID = request.getParameter("DID");
		String ID = request.getParameter("ID");
		String str = "{\"statusCode\":200,\"message\":\""
				+ DID
				+ "删除成功\",\"callbackType\":\"forward\",\"forwardUrl\":\"/Dict/Options.do?ID="
				+ ID + "\"}";

		dictService.Del(DID);
		try {
			response.getWriter().print(str);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
